package com.example.znxb.zswd.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZCF
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/zswd/zswd")
public class ZswdController {

    private static final Logger logger = LoggerFactory.getLogger(ZswdController.class);
    private final RestTemplate restTemplate = new RestTemplate();
    private static final String AI_API_URL = "http://10.10.14.53:8080/api/ai/generate";

    @GetMapping("/generate")
    public ResponseEntity<String> generateResponse(@RequestParam String prompt) {
        try {
            // 构建完整的 URL
            String url = AI_API_URL + "?prompt=" + prompt;
            logger.info("准备调用接口，URL: {}", url);
            
            // 发送请求并获取响应
            ResponseEntity<String> response = restTemplate.getForEntity(
                url,
                String.class
            );
            
            logger.info("接口调用成功，响应状态码: {}", response.getStatusCode());
            return response;
        } catch (Exception e) {
            logger.error("调用接口失败", e);
            // 返回错误信息
            return ResponseEntity.badRequest().body("调用接口失败: " + e.getMessage());
        }
    }
}
